<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Jenis extends CI_Controller
{
	function __construct(){
		parent::__construct();	
		$this->load->library('form_validation');			
		$this->load->model('jenis_model');
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('session');		
	}
	
	function index()
	{
			if($this->session->userdata('nama') == null){
				redirect('welcome');
			}
			else{
			//ver lib
		
			/*
			 * 0 - display name
			 * 1 - width
			 * 2 - sortable
			 * 3 - align
			 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
			 */
			$colModel['NO'] = array('NO',40,FALSE,'center',0);
			$colModel['UBAH'] = array('EDIT',50,FALSE,'center',0);
			$colModel['JENIS_KODE'] = array('KODE',120,TRUE,'center',2);
			$colModel['JENIS_NAMA'] = array('NAMA',120,TRUE,'center',2);
			$colModel['JENIS_KETERANGAN'] = array('KETERANGAN',120,TRUE,'center',2);
					
			
			/*
			 * Aditional Parameters
			 */
			$gridParams = array(
			'width' => 'auto',
			'height' => '335',
			'rp' => 15,
			'rpOptions' => '[10,15,20,25,40]',
			'pagestat' => 'Displaying: {from} to {to} of {total} items.',
			'blockOpacity' => 0.5,
			'title' => 'Daftar Keseluruhan Jenis'
			);
			
			/*
			 * 0 - display name
			 * 1 - bclass
			 * 2 - onpress
			 */
			$buttons[] = array('Tambah Data','add','jenis_js');
			$buttons[] = array('Hapus Data','delete','jenis_js');
			$buttons[] = array('separator');
			$buttons[] = array('Pilih Semua','add','jenis_js');
			$buttons[] = array('Batal Memilih Semua','delete','jenis_js');
			$buttons[] = array('separator');
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("/jenis/jenis_grid/"),$colModel,'no','asc',$gridParams,$buttons);
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"
			<script type='text/javascript'>
						
			function jenis_js(com,grid)
			{
				if (com=='Pilih Semua')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Batal Memilih Semua')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Tambah Data')
				{
					location.href='".base_url()."index.php/jenis/input_jenis/'; 
				}
				
				if (com=='Hapus Data')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/jenis/delete_jenis')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			}
			</script>
			";
		
		/* $data['notification'] = "";
		if($this->session->userdata('notification')!=''){
			$data['notification'] = "
				<script>
					$(document).ready(function() {
						$.growlUI('Pesan :', '".$this->session->userdata('notification')."'); 
					});
				</script>
			";
		}//end if */	
		
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
		//$this->load->view('grid',$data);
		//$this->load->view('flexigrid',$data);
		}
	}
	
	function jenis_grid()
	{
	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('JENIS_KODE','JENIS_NAMA','JENIS_KETERANGAN','JENIS_ID_PARENT');
		
		$this->flexigrid->validate_post('JENIS_ID','asc',$valid_fields);

		$records = $this->jenis_model->get_jenis_grid();
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 1;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		foreach ($records['records']->result() as $row)
		{

			$record_items[] = array($row->JENIS_ID,
			$no,
			'<a href=\''.site_url().'/jenis/edit_jenis/'.$row->JENIS_ID.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> ',
			$row->JENIS_KODE,
			$row->JENIS_NAMA,
			$row->JENIS_KETERANGAN									
			);
			$no++;
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	function cek_validasi() {	
			// Setting Rules
			$this->form_validation->set_rules('jenis_kode', 'Kode Jenis', 'required|is_unique');
			/*
			$this->form_validation->set_rules('perihal', 'Perihal', 'required');
			$this->form_validation->set_rules('jenis_kontrak', 'Jenis Kontrak', 'callback_cek_jenis_kontrak');
			$this->form_validation->set_rules('valuta', 'Valuta', 'callback_cek_valuta');
			$this->form_validation->set_rules('lokasi_kontrak', 'Lokasi Kontrak', 'callback_cek_lokasi_kontrak');*/
			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			$this->form_validation->set_message('is_unique', 'Data sudah ada dalam database');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
	}
	
	function input_jenis()
    {
 		if ($this->cek_validasi() == FALSE){
			$records = $this->jenis_model->count_all_jenis();
			$counter = $records+1;

 			$data['status'] = 'new';
 			$data['jenis_id'] = $counter;	
			$data['content'] = $this->load->view('form_jenis',$data,TRUE);
			$this->load->view('main',$data);
		}
		else{
			$this->insert_jenis();
		}            
    }
	
	function insert_jenis(){
		$records = $this->jenis_model->count_all_jenis();
		$counter = $records+1;

		$data = array(
			'JENIS_ID'	 		=> $counter,
			'JENIS_KODE'	 		=> $this->input->post('jenis_kode'),
			'JENIS_NAMA'	 	=> $this->input->post('jenis_nama'),
			'JENIS_KETERANGAN'				=> $this->input->post('jenis_keterangan'),
			'JENIS_ID_PARENT'			=> $this->input->post('jenis_id_parent'),
			'JENIS_STATUS'		=> 1
		);
		
		$this->jenis_model->insert_jenis($data);
		redirect('jenis'); 
	}
	
	function edit_jenis($id_jenis)
    {
 			if ($this->cek_validasi() == FALSE){
 				$jenis = $this->jenis_model->get_jenisID($id_jenis);

 				foreach($jenis as $row){
           			$data['jenis_kode'] = $row->JENIS_KODE;
            		$data['jenis_nama'] = $row->JENIS_NAMA;
            		$data['jenis_keterangan'] = $row->JENIS_KETERANGAN;
				}			

				$data['jenis_id'] = $id_jenis;			
				$data['status'] = 'edit';		
				$data['jenis'] = $jenis;
				$data['content'] = $this->load->view('form_jenis',$data,true);
				$this->load->view('main',$data);
			}
			else{
				$this->update_jenis($id_jenis);
			}
    }
	
	function update_jenis($id_jenis){		
			$data = array(
				'JENIS_KODE'	 		=> $this->input->post('jenis_kode'),
				'JENIS_NAMA'	 	=> $this->input->post('jenis_nama'),
				'JENIS_KETERANGAN'				=> $this->input->post('jenis_keterangan')
			);
			
			$this->jenis_model->update_jenis($data,$id_jenis);
			redirect('jenis'); 		 
	}
	
	//Delete
	function delete_jenis()
	{
		$jenis_ids_post_array = explode(",",$this->input->post('items'));
		$msg = '';
		foreach($jenis_ids_post_array as $index => $jenis_id){
			if (isset($jenis_id) && $jenis_id != "") {
				$this->jenis_model->delete_jenis($jenis_id);
						
			
			$msg .= "Jenis dengan id (".$jenis_id.") telah berhasil dihapus.\n";
			}
		}
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($msg);
	}

}//end of class